//
//  ALStatusRetweetView.swift
//  ALSinaWeiBoSwift
//
//  Created by 柴瑞军 on 16/4/15.
//  Copyright © 2016年 myCompany. All rights reserved.
//

import UIKit

class ALStatusRetweetView: UIView {

    //定义被转发微博的模型
    var retweekStatus :ALStatus?{
        didSet{
//        当别人设置微博转发数据的时候,将微博内容设置给控件的属性
            let retweekName = retweekStatus?.user?.screen_name ?? "被转发的微博用户名"
            
            let retweetContent = retweekStatus?.text ?? "被转发的微博内容"
            
            contentLabel.text  = "@\(retweekName):\(retweetContent)"
            
            pictureView.pictureURLs = retweekStatus?.storePicutreURLs
//        当没有图片的时候进行约束的更新
            pictureView.snp_updateConstraints { (make) in
                let margin = retweekStatus?.storePicutreURLs==nil ? 0 : ALIconViewMargin
                make.top.equalTo(contentLabel.snp_bottom).offset(margin)
            }
        }
    }
    
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        backgroundColor = UIColor.randomColor()
        
//        添加UI控件
        preparaUI()
    }
    
    private func preparaUI(){
    //添加UI
        addSubview(bgBtnView)
        addSubview(contentLabel)
        addSubview(pictureView)
        
//        设置约束
        bgBtnView.snp_makeConstraints { (make) in
            make.edges.equalTo(self)
        }
        
        contentLabel.snp_makeConstraints { (make) in
            make.leading.top.equalTo(self).offset(ALIconViewMargin)
            make.trailing.equalTo(self).offset(-ALIconViewMargin)
        }
        pictureView.snp_makeConstraints { (make) in
            make.leading.equalTo(self).offset(ALIconViewMargin)
//            make.trailing.equalTo(self).offset(-ALIconViewMargin)
            make.top.equalTo(contentLabel.snp_bottom).offset(ALIconViewMargin)
            make.size.equalTo(CGSizeZero)
        }
        
        self.snp_makeConstraints { (make) in
            make.bottom.equalTo(pictureView).offset(ALIconViewMargin)
        }
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    
    //MARK: - 懒加载控件
//    被转发微博的底部button
    private lazy var bgBtnView:UIButton = {
    let button = UIButton()
    
        button.setBackgroundImage(UIImage.init(named: "retweet_background"), forState: UIControlState.Normal)
        
        button.setBackgroundImage(UIImage(named:"retweet_background_highlighted"), forState: UIControlState.Highlighted)
        return button
    }()
    //微博内容的label
    private lazy var contentLabel: UILabel = {
    let label  = UILabel(fontSize: ALstatusContentFontSize, textColor: UIColor.darkGrayColor())
        label.numberOfLines = 0
        // 换行宽度
        label.preferredMaxLayoutWidth = UIScreen.mainScreen().bounds.width - 2 * ALIconViewMargin
        return label
    }()
    //微博的配图视图
    private lazy var pictureView: ALPictureView = ALPictureView()
}
